package com.course.server.config;


import com.course.server.mapper.my.MyDiscussPostMapper;
import com.course.server.mapper.elasticsearch.DiscussPostRepository;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.IOException;

//import com.course.business.controller.web.discusspost.config.DiscussPostRepository;

@Configuration
public class EsConfig {
    @Value("${elasticSearch.url}")
    private String esUrl;
    @Value("${elasticsearch.indices}")
    String esIndices;

    @Autowired
    private MyDiscussPostMapper discussMapper;

    @Autowired
    private DiscussPostRepository discussRepository;

    //localhost:9200 写在配置文件中就可以了
    @Bean
    RestHighLevelClient client() {
        ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo(esUrl)//elasticsearch地址
                .build();

        return RestClients.create(clientConfiguration).rest();
    }
    @Qualifier("client")
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @PostConstruct
    public void init() throws Exception {
            System.out.println("初始化ElasticSearch里的数据");

        client();

        System.out.println("走完");

        //建立表
        if(!existsIndex(esIndices)){
            //createIndex(esIndices);
        }else{
            deleteIndex(esIndices);
        }

        //把所有帖子（List<DiscussPost>）存入es的discusspost索引（es的索引相当于数据库的表）
        //这个插入顺序是按"最新0/热门1"来插入都无所谓，因为搜索的时候，会在那里按照"热门"来搜索数据。
        discussRepository.saveAll(
                discussMapper.selectAllDiscussPosts(1,"","")
        );//userId=0时表示全查。

        System.out.println("走完");

    }

     @PreDestroy
    public void destroy() throws IOException {
            System.out.println("销毁ElasticSearch里的数据");

        //删除表中数据，销毁表
        deleteIndex(esIndices);
    }
    ////////////////////////////////
    //判断索引是否存在
   public boolean existsIndex(String index) throws IOException {
        GetIndexRequest request = new GetIndexRequest(index);
        boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
        return exists;
    }

    //创建索引
    public boolean createIndex(String index) throws IOException {
        CreateIndexRequest request = new CreateIndexRequest(index);
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
        return createIndexResponse.isAcknowledged();
    }

    //删除索引
    public boolean deleteIndex(String index) throws IOException {
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index);
        AcknowledgedResponse response = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        return response.isAcknowledged();
    }
}
